package com.transport.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.*;

import java.util.Map;

@Activate(group = {CommonConstants.CONSUMER,CommonConstants.PROVIDER})
public class TransportIPFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        String ip = RpcContext.getContext().getAttachment("ip");

        if ( !StringUtils.isEmpty(ip) ) {
            IPUtils.setIp(ip);
        } else {
            RpcContext.getContext().setAttachment("ip", IPUtils.getIp());
        }

        return  invoker.invoke(invocation);

    }

}
